package com.ruoyi.system.service.impl.smy;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.smy.MallGoodsAttribute;
import com.ruoyi.system.mapper.smy.MallGoodsAttributeMapper;
import com.ruoyi.system.service.smy.IMallGoodsAttributeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class MallGoodsAttributeServiceImpl implements IMallGoodsAttributeService {
	@Resource
	private MallGoodsAttributeMapper mallGoodsAttributeMapper;

	@Override
	public MallGoodsAttribute selectMallGoodsAttributeById(Long id) {
		return mallGoodsAttributeMapper.selectMallGoodsAttributeById(id);
	}

	@Override
	public List<MallGoodsAttribute> selectMallGoodsAttributeList(MallGoodsAttribute mallGoodsAttribute) {
		return mallGoodsAttributeMapper.selectMallGoodsAttributeList(mallGoodsAttribute);
	}

	@Override
	public int insertMallGoodsAttribute(MallGoodsAttribute mallGoodsAttribute) {
		return mallGoodsAttributeMapper.insertMallGoodsAttribute(mallGoodsAttribute);
	}

	@Override
	public int updateMallGoodsAttribute(MallGoodsAttribute mallGoodsAttribute) {
		return mallGoodsAttributeMapper.updateMallGoodsAttribute(mallGoodsAttribute);
	}
	@Override
	public int deleteMallGoodsAttributeByIds(Long[] ids) {
		return mallGoodsAttributeMapper.deleteMallGoodsAttributeByIds(ids);
	}

	@Override
	public int deleteMallGoodsAttributeById(Long id) {
		return mallGoodsAttributeMapper.deleteMallGoodsAttributeById(id);
	}

	@Override
	public List<MallGoodsAttribute> selectAttributeParentPage(MallGoodsAttribute mallGoodsAttribute) {
		return mallGoodsAttributeMapper.selectAttributeParentPage(mallGoodsAttribute);
	}

	@Override
	public List<MallGoodsAttribute> getSubAttributeList(MallGoodsAttribute attribute) {
		MallGoodsAttribute queryDto = new MallGoodsAttribute();
		queryDto.setParentId(attribute.getId());
		queryDto.setStatus(0L);
		return mallGoodsAttributeMapper.selectMallGoodsAttributeList(queryDto);
	}


	@Override
	public void saveAttributeWithSubList(MallGoodsAttribute mallGoodsAttribute) {
		List<MallGoodsAttribute> subList = mallGoodsAttribute.getGoodsAttributeList();
		if(Objects.isNull(mallGoodsAttribute.getId())){
			mallGoodsAttribute.setType(1L);
			mallGoodsAttribute.setStatus(0L);
			mallGoodsAttribute.setIssuingSupplierAttributeId(0L);
			mallGoodsAttributeMapper.insertMallGoodsAttribute(mallGoodsAttribute);
		}
		if(Objects.nonNull(mallGoodsAttribute.getId())){
			mallGoodsAttributeMapper.updateMallGoodsAttribute(mallGoodsAttribute);
		}
		//组装规格类目规格值
		buildSubList(mallGoodsAttribute,subList);
	}

	private void buildSubList(MallGoodsAttribute mallGoodsAttribute, List<MallGoodsAttribute> subList) {
		List<MallGoodsAttribute> dbList = getSubAttributeList(mallGoodsAttribute);
		List<MallGoodsAttribute> deleteList = CollectionUtil.newArrayList();
		List<MallGoodsAttribute> saveList = CollectionUtil.newArrayList();
		Map<Long,MallGoodsAttribute> subMap = subList.stream().collect(Collectors.toMap(MallGoodsAttribute::getId, a -> a,(k1, k2)->k1));
		for(MallGoodsAttribute attribute : subList){
			if(Objects.isNull(attribute.getId())){
				attribute.setParentId(mallGoodsAttribute.getId());
				attribute.setStatus(0L);
				attribute.setType(1L);
				attribute.setIssuingSupplierAttributeId(0L);
				saveList.add(attribute);
			}
		}
		for(MallGoodsAttribute attribute : dbList){
			if(!subMap.containsKey(attribute.getId())){
				attribute.setStatus(2L);
				deleteList.add(attribute);
			}
		}
		if(CollectionUtil.isNotEmpty(saveList)){
			saveList.forEach(item->mallGoodsAttributeMapper.insertMallGoodsAttribute(item));
		}
		if(CollectionUtil.isNotEmpty(deleteList)){
			deleteList.forEach(item->mallGoodsAttributeMapper.updateMallGoodsAttribute(item));
		}
	}
}
